package ex2.LinkedAndArray;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

import static ex2.LinkedAndArray.ex4.*;

public class ex5 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入数字：");
        String a = scanner.nextLine();

        StringTokenizer stringTokenizer = new StringTokenizer(a);
        int[] numbers = new int[stringTokenizer.countTokens()];
        int nGuoKai =0;

        //  将输入的数字存入数组当中。
        while (stringTokenizer.hasMoreTokens())
        {
            numbers[nGuoKai] = Integer.valueOf(stringTokenizer.nextToken());
            nGuoKai++;
        }

        PrintArray(numbers);
        System.out.println();
        System.out.println("个数为：" + nGuoKai);

        //从文件中读取
        File file = new File("D:\\IdeaProjects\\GK20172301_JavaProgramming\\src\\ex2\\LinkedAndArray\\sort.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str = "";
        str = bufferedReader.readLine();
        bufferedReader.close();

        StringTokenizer stringTokenizer1 = new StringTokenizer(str);
        int[] numbers1 = new int[stringTokenizer1.countTokens()];
        int y = 0;
        while (stringTokenizer1.hasMoreTokens()) {
            numbers1[y] = Integer.parseInt(stringTokenizer1.nextToken());
            y++;
        }

        int p = numbers1[0];
        int q = numbers1[1];

        // 在第五位插入p
        int[] numbers2 = Insert(numbers,5,p);
        System.out.println("在第五位插入文件中的第一个数：");
        PrintArray(numbers2);
        nGuoKai++;
        System.out.println();
        System.out.println("个数为：" + nGuoKai);
        System.out.println();

        // 在第零位插入q
        int[] numbers3 = Insert(numbers2,0,q);
        System.out.println("在第零位插入文件中的第二个数：");
        PrintArray(numbers3);
        nGuoKai++;
        System.out.println();
        System.out.println("个数为：" + nGuoKai);
        System.out.println();

        // 删除插入的p
        int[] numbers4 = Delete(numbers3,6);
        System.out.println("删除插入的第一个数：");
        PrintArray(numbers4);
        nGuoKai--;
        System.out.println();
        System.out.println("个数为： " + nGuoKai);
        System.out.println();

        // 选择排序
        System.out.println("使用选择排序：");
        selectionSort(numbers4);
//        PrintArray(numbers4);
//        nGuoKai = numbers4.length;
//        System.out.println("个数为： " + nGuoKai);

    }

    public static void selectionSort(int[] list)
    {
        int min;
        int temp;
        int nGuoKai;

        for (int index = 0; index < list.length-1; index++)
        {
            min = index;
            for (int scan = index+1; scan < list.length; scan++)
            {
                if (list[scan] < list[min]) {
                    min = scan;
                }
            }
                temp = list[min];
                list[min] = list[index];
                list[index] = temp;
                PrintArray(list);
                System.out.println();
                nGuoKai = list.length;
                System.out.println("个数为： " + nGuoKai);
        }
    }
}
